<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- analogclockwindow.qdoc -->
  <title>Analog Clock Window Example | Qt GUI 5.14.2</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.14</a></td><td ><a href="qtgui-index.html">Qt GUI</a></td><td >Analog Clock Window Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtgui-index.html">Qt 5.14.2 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#analogclockwindow-class-definition">AnalogClockWindow Class Definition</a></li>
<li class="level1"><a href="#analogclock-class-implementation">AnalogClock Class Implementation</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Analog Clock Window Example</h1>
<span class="subtitle"></span>
<!-- $$$analogclock-brief -->
<p>The Analog Clock Window example shows how to draw the contents of a custom window.</p>
<!-- @@@analogclock -->
<!-- $$$analogclock-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/analogclock-window-example.png" alt="Screenshot of the Analog" /></p><p>Clock Window example</p>
<p>This example demonstrates how the transformation and scaling features of <a href="qpainter.html">QPainter</a> can be used to make drawing easier.</p>
<a name="analogclockwindow-class-definition"></a>
<h4 id="analogclockwindow-class-definition">AnalogClockWindow Class Definition</h4>
<p>The <code>AnalogClockWindow</code> class provides a clock with hour and minute hands that is automatically updated every few seconds. We make use of the RasterWindow from the <a href="qtgui-rasterwindow-example.html">Raster Window Example</a> and reimplement the <code>render</code> function to draw the clock face:</p>
<pre class="cpp">

  <span class="keyword">class</span> AnalogClockWindow : <span class="keyword">public</span> RasterWindow
  {
  <span class="keyword">public</span>:
      AnalogClockWindow();

  <span class="keyword">protected</span>:
      <span class="type">void</span> timerEvent(<span class="type"><a href="../qtcore/qtimerevent.html">QTimerEvent</a></span> <span class="operator">*</span>) override;
      <span class="type">void</span> render(<span class="type"><a href="qpainter.html">QPainter</a></span> <span class="operator">*</span>p) override;

  <span class="keyword">private</span>:
      <span class="type">int</span> m_timerId;
  };

</pre>
<a name="analogclock-class-implementation"></a>
<h4 id="analogclock-class-implementation">AnalogClock Class Implementation</h4>
<pre class="cpp">

  AnalogClockWindow<span class="operator">::</span>AnalogClockWindow()
  {
      setTitle(<span class="string">&quot;Analog Clock&quot;</span>);
      resize(<span class="number">200</span><span class="operator">,</span> <span class="number">200</span>);

      m_timerId <span class="operator">=</span> startTimer(<span class="number">1000</span>);
  }

</pre>
<p>We set a title on the window and resize to a reasonable size. Then we start a timer which we will use to redraw the clock every second.</p>
<pre class="cpp">

  <span class="type">void</span> AnalogClockWindow<span class="operator">::</span>timerEvent(<span class="type"><a href="../qtcore/qtimerevent.html">QTimerEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>timerId() <span class="operator">=</span><span class="operator">=</span> m_timerId)
          renderLater();
  }

</pre>
<p>The timerEvent function is called every second as a result of our startTimer call. Making use of the convenience in the base class, we schedule the window to be repainted.</p>
<p>Checking the timer's id is not strictly needed as we only have one active timer in this instance, but it is good practice to do so.</p>
<pre class="cpp">

  <span class="type">void</span> AnalogClockWindow<span class="operator">::</span>render(<span class="type"><a href="qpainter.html">QPainter</a></span> <span class="operator">*</span>p)
  {
      <span class="keyword">static</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> hourHand<span class="operator">[</span><span class="number">3</span><span class="operator">]</span> <span class="operator">=</span> {
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">7</span><span class="operator">,</span> <span class="number">8</span>)<span class="operator">,</span>
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="operator">-</span><span class="number">7</span><span class="operator">,</span> <span class="number">8</span>)<span class="operator">,</span>
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="operator">-</span><span class="number">40</span>)
      };
      <span class="keyword">static</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> minuteHand<span class="operator">[</span><span class="number">3</span><span class="operator">]</span> <span class="operator">=</span> {
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">7</span><span class="operator">,</span> <span class="number">8</span>)<span class="operator">,</span>
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="operator">-</span><span class="number">7</span><span class="operator">,</span> <span class="number">8</span>)<span class="operator">,</span>
          <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="operator">-</span><span class="number">70</span>)
      };

      <span class="type"><a href="qcolor.html">QColor</a></span> hourColor(<span class="number">127</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">127</span>);
      <span class="type"><a href="qcolor.html">QColor</a></span> minuteColor(<span class="number">0</span><span class="operator">,</span> <span class="number">127</span><span class="operator">,</span> <span class="number">127</span><span class="operator">,</span> <span class="number">191</span>);

</pre>
<p>Before we set up the painter and draw the clock, we first define two lists of <a href="../qtcore/qpoint.html">QPoint</a>s and two <a href="qcolor.html">QColor</a>s that will be used for the hour and minute hands. The minute hand's color has an alpha component of 191, meaning that it's 75% opaque.</p>
<pre class="cpp">

      p<span class="operator">-</span><span class="operator">&gt;</span>setRenderHint(<span class="type"><a href="qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing);

</pre>
<p>We call <a href="qpainter.html#setRenderHint">QPainter::setRenderHint</a>() with <a href="qpainter.html#RenderHint-enum">QPainter::Antialiasing</a> to turn on antialiasing. This makes drawing of diagonal lines much smoother.</p>
<pre class="cpp">

      p<span class="operator">-</span><span class="operator">&gt;</span>translate(width() <span class="operator">/</span> <span class="number">2</span><span class="operator">,</span> height() <span class="operator">/</span> <span class="number">2</span>);

      <span class="type">int</span> side <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMin">qMin</a>(width()<span class="operator">,</span> height());
      p<span class="operator">-</span><span class="operator">&gt;</span>scale(side <span class="operator">/</span> <span class="number">200.0</span><span class="operator">,</span> side <span class="operator">/</span> <span class="number">200.0</span>);

</pre>
<p>The translation moves the origin to the center of the window, and the scale operation ensures that the following drawing operations are scaled to fit within the window. We use a scale factor that let's us use x and y coordinates between -100 and 100, and that ensures that these lie within the length of the window's shortest side.</p>
<p>To make our code simpler, we will draw a fixed size clock face that will be positioned and scaled so that it lies in the center of the window.</p>
<p>We also determine the length of the window's shortest side so that we can fit the clock face inside the window.</p>
<p>The painter takes care of all the transformations made during the rendering, and ensures that everything is drawn correctly. Letting the painter handle transformations is often easier than performing manual calculations.</p>
<p class="centerAlign"><img src="images/analogclockwindow-viewport.png" alt="" /></p><p>We draw the hour hand first, using a formula that rotates the coordinate system counterclockwise by a number of degrees determined by the current hour and minute. This means that the hand will be shown rotated clockwise by the required amount.</p>
<pre class="cpp">

      p<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="qt-sub-qtgui.html">Qt</a></span><span class="operator">::</span>NoPen);
      p<span class="operator">-</span><span class="operator">&gt;</span>setBrush(hourColor);

</pre>
<p>We set the pen to be <a href="../qtcore/qt.html#PenStyle-enum">Qt::NoPen</a> because we don't want any outline, and we use a solid brush with the color appropriate for displaying hours. Brushes are used when filling in polygons and other geometric shapes.</p>
<pre class="cpp">

      <span class="type"><a href="../qtcore/qtime.html">QTime</a></span> time <span class="operator">=</span> <span class="type"><a href="../qtcore/qtime.html">QTime</a></span><span class="operator">::</span>currentTime();

      p<span class="operator">-</span><span class="operator">&gt;</span>save();
      p<span class="operator">-</span><span class="operator">&gt;</span>rotate(<span class="number">30.0</span> <span class="operator">*</span> ((time<span class="operator">.</span>hour() <span class="operator">+</span> time<span class="operator">.</span>minute() <span class="operator">/</span> <span class="number">60.0</span>)));
      p<span class="operator">-</span><span class="operator">&gt;</span>drawConvexPolygon(hourHand<span class="operator">,</span> <span class="number">3</span>);
      p<span class="operator">-</span><span class="operator">&gt;</span>restore();

</pre>
<p>We save and restore the transformation matrix before and after the rotation because we want to place the minute hand without having to take into account any previous rotations.</p>
<pre class="cpp">

      p<span class="operator">-</span><span class="operator">&gt;</span>setPen(hourColor);

      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> <span class="number">12</span>; <span class="operator">+</span><span class="operator">+</span>i) {
          p<span class="operator">-</span><span class="operator">&gt;</span>drawLine(<span class="number">88</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">96</span><span class="operator">,</span> <span class="number">0</span>);
          p<span class="operator">-</span><span class="operator">&gt;</span>rotate(<span class="number">30.0</span>);
      }

</pre>
<p>We draw markers around the edge of the clock for each hour. We draw each marker then rotate the coordinate system so that the painter is ready for the next one.</p>
<pre class="cpp">

      p<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="qt-sub-qtgui.html">Qt</a></span><span class="operator">::</span>NoPen);
      p<span class="operator">-</span><span class="operator">&gt;</span>setBrush(minuteColor);
      p<span class="operator">-</span><span class="operator">&gt;</span>save();
      p<span class="operator">-</span><span class="operator">&gt;</span>rotate(<span class="number">6.0</span> <span class="operator">*</span> (time<span class="operator">.</span>minute() <span class="operator">+</span> time<span class="operator">.</span>second() <span class="operator">/</span> <span class="number">60.0</span>));
      p<span class="operator">-</span><span class="operator">&gt;</span>drawConvexPolygon(minuteHand<span class="operator">,</span> <span class="number">3</span>);
      p<span class="operator">-</span><span class="operator">&gt;</span>restore();

</pre>
<p>The minute hand is rotated in a similar way to the hour hand.</p>
<pre class="cpp">

      p<span class="operator">-</span><span class="operator">&gt;</span>setPen(minuteColor);

      <span class="keyword">for</span> (<span class="type">int</span> j <span class="operator">=</span> <span class="number">0</span>; j <span class="operator">&lt;</span> <span class="number">60</span>; <span class="operator">+</span><span class="operator">+</span>j) {
          <span class="keyword">if</span> ((j <span class="operator">%</span> <span class="number">5</span>) <span class="operator">!</span><span class="operator">=</span> <span class="number">0</span>)
              p<span class="operator">-</span><span class="operator">&gt;</span>drawLine(<span class="number">92</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">96</span><span class="operator">,</span> <span class="number">0</span>);
          p<span class="operator">-</span><span class="operator">&gt;</span>rotate(<span class="number">6.0</span>);
      }

</pre>
<p>Again, we draw markers around the edge of the clock, but this time to indicate minutes. We skip multiples of 5 to avoid drawing minute markers on top of hour markers.</p>
<p><a href="https://code.qt.io/cgit/qt/qtbase.git/tree/examples/gui/analogclock?h=5.14">Example project @ code.qt.io</a></p>
</div>
<!-- @@@analogclock -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
